package com.tencet.dao.impl;

import com.tencet.dao.AccountTwoDAO;
import com.tencet.entity.AccountOne;
import com.tencet.entity.AccountTwo;
import com.tencet.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * @author: Fame
 * @date: 2022-07-20 16:25
 * @description:
 */
public class AccountTwoDAOImpl implements AccountTwoDAO {

    private QueryRunner runner = new QueryRunner(JDBCUtils.getDataSource());
    @Override
    public AccountTwo getAccountTwoById(String id) {
        String sql = "select account_id2,card_type,balance,card_num,phone_num from bank_account2 where account_id2 = ?";

        try {
            AccountTwo accountTwo = runner.query(sql, new BeanHandler<AccountTwo>(AccountTwo.class), id);
            System.out.println(accountTwo);
            return accountTwo;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int insertBean(AccountTwo accountTwo) {
        String sql = "insert into bank_account2 values(?,?,?,?,?)";
        try {
           return runner.update(sql,accountTwo.getAccount_id2(),accountTwo.getCard_type(),accountTwo.getBalance(),accountTwo.getCard_num(),accountTwo.getPhone_num());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return -1;
    }

    @Override
    public int getBalance(String card_num2) {
        String sql = "select balance from bank_account2 where card_num = ?";
        try {
             return (Integer) runner.query(sql, new ScalarHandler(), card_num2);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addBalance(Integer sum, String car_num2) {
        String sql = "update bank_account2 set balance = ? where card_num = ?";
        try {
            return runner.update(sql,sum,car_num2);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }



    @Override
    public String getIdByCardNum(String card_num) {
        String sql = "select account_id2 from bank_account2 where card_num = ?";
        try {
            return ""+runner.query(sql,new ScalarHandler(),card_num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
